<?php
namespace Modules\Admin\Http\Controllers;
use Illuminate\Http\Request;
class ModulesController extends CommonController
{
    /**
     * @name  列表
     */
    public function index()
    {
        return view('admin::modules.index');
    }
    /**
     * @name  列表数据
     */
    public function data(Request $request)
    { 	
		$data = get_dir(base_path('Modules'));
		$arr = [];
		if($data){
			foreach($data as $k=>$v){
				$arr[$k]['value'] = config(strtolower($v).'.name');
				$arr[$k]['key'] = $v;
			}
		}
		
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'data'=>$arr,'rel'=>1]);
    }
    /**
     * @name  编辑页面
     */
    public function edit($key)
    {
        return view('admin::modules.edit',compact('key'));
    }
	/**
     * @name  编辑数据列表
     */
	public function edit_data($key)
    {
		$config_url = base_path('Modules/'.$key.'/Config/config.php');
		$string = file_get_contents($config_url);
		$string = preg_split('/\n+/',$string);
		unset($string[0]);
		unset($string[1]);
		foreach ($string as $k => $one) {
            if (preg_match('/^(\/\/\s)/', $one) === 1 || preg_match('/^([\\n\\r]+)/', $one)) {
                continue;
            }
            $entry = explode("=>", $one, 2);
            if (!empty($entry[0]) && trim($entry[0]) != ''){
				if(isset($entry['1']) && strstr($entry['1'],'//')){
					$entry['1'] = explode("//",$entry['1']);
				}else{
					$entry['1']['0'] = '';
				}
				$array[] = [
				'id' => $k - 1, 
				'key' => rtrim(ltrim(trim($entry[0]),"'"),"'"), 
				'value' => isset($entry['1']['0']) ? rtrim(ltrim(trim($entry['1']['0']),"'"),"',") : null,
				'name'=>isset($entry['1']['1']) ? substr(rtrim(ltrim(trim($entry['1']['1']),"'"),"',"),0,-2) : null
				];
            }
        }
		array_pop($array);
		unset($array[0]);
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'data'=>$array,'rel'=>1]);
    }
    /**
     * @name  编辑数据处理
     */
    public function modules_update(Request $request,$modules_key)
    {
        $data = $request->all();
        if($this->setconfig($data['key'],$data['value'],$data['name'],$modules_key)){
            return response()->json([
                'status'=>1,
                'error' => '',
                'msg' => config('admin.update_success')
            ],200);
        }else{
            return response()->json([
                'status'=>0,
                'error' => config('admin.update_error'),
                'msg' => ''
            ],201);
        }
    }
    /**
	 *   修改config配置文件
	 *   @method  setconfig
	 *   deep
	 *   @param   [array]     $key  [键]
	 *   @param   [array]     $value  [值]
	 *   @return  [boolean]  [结果]
	 */
	private function setconfig($key,$value,$name,$modules_key)
	{
		$keys = '/\'' . $key . '\'(.*?)\|\|/';
		$values = "'". $key. "'". "=>" . "'".$value ."',"."//" .$name .'||';
		$fileurl = base_path('Modules/'.$modules_key.'/Config/config.php');
		$string = file_get_contents($fileurl); //加载配置文件
		$string = preg_replace($keys,$values, $string);
		// 正则查找替换
		file_put_contents($fileurl, $string); // 写入配置文件
		return true;
		
	}

}